package com.star.spring.aop.invocation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * @ClassName MyinvocationHandler
 * @Author star
 * @Date 2024/10/18 11:22
 */

@Service
@Slf4j

public class MyInvocationHandler implements InvocationHandler {

    private Object object;
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log.info("before");
        log.info("proxy:{}", proxy);
        log.info("method:{}", method.getName());
        log.info("args:{}", Arrays.toString(args));
        Object invoke = method.invoke(object, args);
        log.info("after");
        return invoke;
    }

    /**
     * 获取代理类
     * @param target
     * @return
     */
    public Object bind(Object target) {
        this.object = target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }
}
